import areaService from '../services/area';
import {message} from 'antd';
import {EDIT_TYPE_MAP} from "@/pages/DataDef/constants";

export default {
  state: {
    listLoading: false,
    drawerType: EDIT_TYPE_MAP.ADD,
    drawerVisible: false,
    dataList: [],
    selectRec: {
      uid: '',
      code: '',
      name: '',
    },
  },
  reducers: {
    update(prevState, payload) {
      return {...prevState, ...payload};
    },
  },
  effects: (dispatch) => ({
    // 主题域
    async getAreaList() {
      dispatch.area.update({listLoading: true});
      const res = await areaService.getAreaList();
      dispatch.area.update({dataList: res.data, listLoading: false});
    },
    showAreaDrawer(type = 1) {
      dispatch.area.update({drawerVisible: true, drawerType: type});
    },
    closeAreaDrawer() {
      dispatch.area.update({drawerVisible: false, drawerType: EDIT_TYPE_MAP.ADD});
    },
    setSelected(rec) {
      dispatch.def.update({selectRec: rec});
    },
    async addAreaInfo(areaInfo) {
      const res = await areaService.addAreaInfo(areaInfo);
      if (res.code > 0) {
        message.info('保存成功');
        dispatch.area.update({drawerVisible: false});
        await dispatch.area.getAreaList();
      }
    },
    async updateAreaInfo(areaInfo) {
      const res = await areaService.updateAreaInfo(areaInfo);
      if (res.data.code > 0) {
        message.info('保存成功');
        await dispatch.area.getAreaList();
      }
    },
    async deleteAreaInfo(areaInfo) {
      const res = await areaService.deleteAreaInfo(areaInfo.uid);
      if (res.data.code > 0) {
        message.info('删除成功');
        await dispatch.area.getAreaList();
      }
    },
  }),
};
